package com.zx.sdk.util;

import android.util.Log;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.Map;

import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Http工具类
 */
public class HttpHelper {
    private static final OkHttpClient mHttpClient = new OkHttpClient();
    private static final OkHttpClient retryHttpClient = new OkHttpClient.Builder().addInterceptor(new RetryInterceptor(2)).build();

    public static void postJSON(String url, String json, Callback callback) {
        postJSON(url, null, json, callback);
    }

    public static void postJSON(String url, Map<String, String> header, String json, Callback callback) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        Request.Builder builder = new Request.Builder();
        builder.addHeader("Content-Type", "application/json");
        if (header != null) {
            for (String key : header.keySet()) {
                String str = header.get(key);
                builder.addHeader(key, str == null ? "" : str);
            }
        }
        builder.url(url)
                .post(body);
        mHttpClient.newCall(builder.build()).enqueue(callback);
    }

    public static void retryPostJSON(String url, Map<String, String> header, String json, Callback callback) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        Request.Builder builder = new Request.Builder();
        builder.addHeader("Content-Type", "application/json");
        if (header != null) {
            for (String key : header.keySet()) {
                String str = header.get(key);
                builder.addHeader(key, str == null ? "" : str);
            }
        }
        builder.url(url)
                .post(body);
        retryHttpClient.newCall(builder.build()).enqueue(callback);
    }

    public static void retryPostJSON(String url, String json, Callback callback) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        Request request = new Request.Builder()
                .addHeader("Content-Type", "application/json")
                .url(url)
                .post(body)
                .build();
        retryHttpClient.newCall(request).enqueue(callback);
    }

    public static void get(String url, Callback callback) {
        mHttpClient.newCall(new Request.Builder().url(url).get().build()).enqueue(callback);
    }

    /**
     * 重试拦截器
     */
    public static class RetryInterceptor implements Interceptor {

        public int maxRetry;// 最大重试次数
        private int retryNum = 0;// 假如设置为3次重试的话，则最大可能请求4次（默认1次+3次重试）

        public RetryInterceptor(int maxRetry) {
            this.maxRetry = maxRetry;
        }

        @NonNull
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Response response = chain.proceed(request);
            while (!response.isSuccessful() && retryNum < maxRetry) {
                retryNum++;
                response.close();
                response = chain.proceed(request);
            }
            return response;
        }
    }
}
